﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Threading;
using System.Xml;

namespace hxSec.StaticPage
{   
    public partial class MainForm : Form
    {
        // 创建回调函数代理
        //delegate void SetCallback(DataRow row);

        private System.Drawing.Icon iconRun;
        private System.Drawing.Icon iconStop;
        private string inteval = "";
        private string intevalToDB = "";
        private string PublicPageInfo = "<!--www.gazxfe.com," + DateTime.Now.ToString() + ",system-->\r\n";
        private int errorcount = 0;    

        private bool BoolAppIsRunning = false;  //是否正在运行
        private bool BoolNeedRun = false;       //是否需要运行

        private string AppAutoRun;
        private string AppAutoRunByTime;
        private string chartFormat;

        private System.Xml.XmlDocument dom = null;
        private DataTable dtList;
        private DataTable dt;
        //private DataTable dtLabel;
        private DataTable tempData;
        private int nowthreadCount;
        private int threadCount;
        private int eachCustomerCount;
        private int waitCustomerCount;
        //private int finishCustomerCount;
        private int AppRunCount = 0;

        ChartData chartData = new ChartData();
        ChartHandler handler = new ChartHandler();
        public ChartQueue queue = new ChartQueue();
       
        public MainForm()
        {
            this.dom = null;
            this.inteval = "";
            this.errorcount = 0;
            this.BoolAppIsRunning = false;
            this.AppAutoRun = "";
            this.nowthreadCount = 0;          

            InitializeComponent();

            //取消控件线程安全保护模式
            Control.CheckForIllegalCrossThreadCalls = false;
          
            try
            {
                this.iconRun = new Icon("ChartGenerator\\AppRun.ico");
                this.iconStop = new Icon("ChartGenerator\\AppStop.ico");               
            }
            catch (Exception exception)
            {
                this.tbAutoInfo.AppendText(exception.ToString());
            }
        }

        // 窗体载入时候发生
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.LoadConfig();
            //notifyIcon.Icon = iconStop;
            if (AppAutoRun.CompareTo("1") == 0)
            {
                BoolNeedRun = true;
                timer1.Interval = 3000;
                timer1.Start();
            }
            else
            {
                BoolNeedRun = false;
            }
        }

        // 窗体大小发生改变时发生
        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (base.WindowState == FormWindowState.Minimized)
            {
                base.Hide();
            }
        }

        #region 载入配置及初始化 datatable
        private void LoadConfig()
        {
            dom = new XmlDocument();
            string filename = Application.StartupPath + @"\ChartGenerator\Config.xml";
            dom.Load(filename);
            AppAutoRun = dom.SelectSingleNode("//AppAutoRun").InnerText;
            AppAutoRunByTime = dom.SelectSingleNode("//AppAutoRunByTime").InnerText;
            chartFormat = dom.SelectSingleNode("//chartFormat").InnerText;
            inteval = dom.SelectSingleNode("//inteval").InnerText;
            intevalToDB = dom.SelectSingleNode("//intevalToDB").InnerText;
            threadCount = int.Parse(dom.SelectSingleNode("//threadCount").InnerText);
            eachCustomerCount = int.Parse(dom.SelectSingleNode("//eachCustomerCount").InnerText);

            this.dt = new DataTable();
            this.dt.Columns.Add(new DataColumn("LABELID"));
            this.dt.Columns.Add(new DataColumn("RELACODE"));
            this.dt.Columns.Add(new DataColumn("RELANAME"));
            this.dt.Columns.Add(new DataColumn("LABELTYPE"));
            this.dt.Columns.Add(new DataColumn("WIDTH"));
            this.dt.Columns.Add(new DataColumn("HEIGHT"));
            this.dt.Columns.Add(new DataColumn("CHARTSTYLE"));
            this.dt.Columns.Add(new DataColumn("LABELNAME"));
            this.dt.Columns.Add(new DataColumn("GRAPHYID"));
            this.dt.Columns.Add(new DataColumn("OPERATIONDATE"));
            this.tempData = new DataTable();
            this.tempData.Columns.Add(new DataColumn("LABELID"));
            this.tempData.Columns.Add(new DataColumn("RELACODE"));
            this.tempData.Columns.Add(new DataColumn("RELANAME"));
            this.tempData.Columns.Add(new DataColumn("LABELTYPE"));
            this.tempData.Columns.Add(new DataColumn("WIDTH"));
            this.tempData.Columns.Add(new DataColumn("HEIGHT"));
            this.tempData.Columns.Add(new DataColumn("CHARTSTYLE"));
            this.tempData.Columns.Add(new DataColumn("LABELNAME"));
            this.tempData.Columns.Add(new DataColumn("GRAPHYID"));
            this.tempData.Columns.Add(new DataColumn("OPERATIONDATE"));
            this.dtList = new DataTable();
            this.dtList.Columns.Add(new DataColumn("ID"));
            this.dtList.Columns.Add(new DataColumn("TYPE"));
            this.dtList.Columns.Add(new DataColumn("ADDRESS"));

            XmlNodeList Addresses = dom.SelectNodes("//Addresses//Address");
            foreach (XmlNode node in Addresses)
            {
                DataRow row = dtList.NewRow();
                row[0] = node.Attributes["id"].Value;
                row[1] = node.Attributes["type"].Value;
                row[2] = node.InnerText;
                dtList.Rows.Add(row);
            }
        }
        #endregion 

        #region 循环处理动态转静态
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timer1.Interval != Convert.ToInt32(inteval) * 1000)
            {
                timer1.Interval = Convert.ToInt32(inteval) * 1000;
            }
            if (BoolNeedRun)
            {
                if (!BoolAppIsRunning)
                {
                    BoolAppIsRunning = true;
                    btnClick.Enabled = false;
                    //notifyIcon.Icon = iconRun;                   
                    timer1.Start();
                    object obj = this.errorcount;
                    Monitor.Enter(obj);
                    this.errorcount = 0;
                    Monitor.Exit(obj);
                    timer3.Start();
                    FnChart2DB();
                    FnDrawChart2Model();
                    if (dt == null || dt.Rows.Count <= 0)
                        BoolAppIsRunning = false;
                }
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (!BoolNeedRun)
            {
                timer2.Stop();
            }

            if ((BoolAppIsRunning) && (dt.Rows.Count > 0))
            {
                AddTreadPool();
            }
            else
            {
                if (nowthreadCount == 0)
                {
                    BoolAppIsRunning = false;
                    timer2.Stop();
                    tbAutoInfo.AppendText("\r\n－－－" + DateTime.Now.ToString() + "本轮账户图表生成结束－－－");
                    AppRunCount += 1;
                }
            }
        }

        #endregion

        // 对线程池的处理
        private void AddTreadPool()
        {
            if (BoolAppIsRunning)
            {
                try
                {
                    while (dt.Rows.Count > 0)
                    {
                        Monitor.Enter(dt);
                        DataRow row = dt.Rows[0];
                        Monitor.Enter(tempData);
                        DataRow row2 = tempData.NewRow();
                        for (int i = 0; i < row.Table.Columns.Count; i++)
                        {
                            row2[i] = row[i].ToString();
                        }
                        tempData.Rows.Add(row2);
                        dt.Rows.RemoveAt(0);
                        Monitor.Exit(tempData);
                        Monitor.Exit(dt);
                        DealRows(row2);
                    }

                    #region 注释
                    //int num2;
                    //int num3;
                    //int count = dt.Rows.Count;
                    //ThreadPool.GetAvailableThreads(out num3, out num2);
                    //if (count >= threadCount)//当前剩余处理数>=总运行数量
                    //{
                    //    count = threadCount;
                    //}
                    //if (count > num2)
                    //{
                    //    count = num2;
                    //}
                    //int num4 = 0;
                    //while (((dt.Rows.Count > 0) && (num4 <= count)) && (nowthreadCount <= threadCount))
                    //{
                    //    num4++;
                    //    nowthreadCount++;
                    //    if (dt.Rows.Count > 0)
                    //    {
                    //        Monitor.Enter(dt);
                    //        DataRow row = dt.Rows[0];
                    //        Monitor.Enter(tempData);
                    //        DataRow row2 = tempData.NewRow();
                    //        for (int i = 0; i < row.Table.Columns.Count; i++)
                    //        {
                    //            row2[i] = row[i].ToString();
                    //        }
                    //        tempData.Rows.Add(row2);
                    //        dt.Rows.RemoveAt(0);
                    //        Monitor.Exit(tempData);
                    //        Monitor.Exit(dt);

                    //        //SetCallback d = new SetCallback(DealRows);
                    //        //this.Invoke(d, new object[] { row2 });

                    //        if (!ThreadPool.QueueUserWorkItem(new WaitCallback(DealRows), row2))
                    //        {
                    //            Thread.Sleep(800);
                    //            return;
                    //        }
                    //    }
                    //}
                    #endregion
                }
                catch (Exception exception)
                {
                    tbAutoInfo.AppendText("\r\n－－" +DateTime.Now.ToString() + " 调用线程池出错 \r\n" + exception.ToString());
                    WriteLog("\r\n－－" + DateTime.Now.ToString() + " 调用线程池出错\r\n" + exception.ToString());

                    Monitor.Exit(tempData);
                    Monitor.Exit(dt);
                }
            }
        }

        /// <summary>
        /// 生成图表执行按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClick_Click(object sender, EventArgs e)
        {
            btnClick.Enabled = false;
            btnPAUSE.Enabled = true;
            BoolNeedRun = true;
            //notifyIcon.Icon = iconRun;
            Form.ActiveForm.Icon = iconRun;
            BoolAppIsRunning = true;
            timer3.Start();
            FnChart2DB();
            timer1.Start();
            FnDrawChart2Model();          
        }

        /// <summary>
        /// 生成图表暂停按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPAUSE_Click(object sender, EventArgs e)
        {
            btnClick.Enabled = true;
            btnPAUSE.Enabled = false;
            BoolNeedRun = false;
            BoolAppIsRunning = false;
            timer1.Stop();
            timer3.Stop();
            //notifyIcon.Icon = iconStop;
            Form.ActiveForm.Icon = iconStop;
        }

        // 需要画标签图的客户信息加载
        private void FnDrawChart2Model()
        {
            bool flag;
            //Monitor.Enter(this.dtLabel);
            try
            {                
                //this.dt = chartData.GetWaitTreatCustomerInfo(eachCustomerCount);
                this.dt = chartData.GetChartLabelData(0);               
                this.waitCustomerCount = this.dt.Rows.Count;
                if (waitCustomerCount > 0)
                {
                    this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 装载客户列表与标签列表成功,即将开始处理－－");
                    this.lbWaitCount.Text = waitCustomerCount.ToString() + " 个";
                    flag = true;
                }
                else
                {                    
                    if (this.tbAutoInfo.Lines.Length > 10)
                        this.tbAutoInfo.Clear();

                    if (this.tbToDBInfo.Lines.Length > 10)
                        this.tbToDBInfo.Clear();

                    this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 没有要处理客户,等待下次轮询－－");
                    flag = false;
                }
            }
            catch (Exception exception)
            {
                this.tbAutoInfo.AppendText("\r\n－－" + exception.ToString());
                flag = false;
            }
            finally
            {
                //Monitor.Exit(this.dtLabel);
            }
            if (flag)
            {
                if (AppRunCount > 1)
                {
                    this.tbAutoInfo.Text = "";
                    AppRunCount = 0;
                }

                this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 装载客户列表成功,本轮生成开始－－");
                progressBar.Minimum = 0;
                progressBar.Maximum = dt.Rows.Count - 1;
                progressBar.Value = 0;
                progressBar.Step = 1;
                //timer3.Start();
                AddTreadPool();
                //timer2.Start();               
            }
        }

        // 生成图表回调函数
        private void DealRows(object odr)
        {
            try
            {
                Monitor.Enter(tempData);
                DataRow row = (DataRow)odr;
                //this.tempData.Rows.Remove(row);
                Monitor.Exit(this.tempData);

                //this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + account + "(" + name + "), 正在处理中...");

                FnCreateAccountChartFile(row);
            }
            catch (Exception exception)
            {
                this.tbAutoInfo.AppendText("\r\n" + exception.ToString());
            }
            finally
            {
                progressBar.PerformStep();
                //this.finishCustomerCount = this.waitCustomerCount - dt.Rows.Count;
                //this.lbFinishCount.Text = finishCustomerCount.ToString() + " 个";
                object obj = this.nowthreadCount;
                Monitor.Enter(obj);
                //this.nowthreadCount--;
                //if (this.nowthreadCount < 0)
                //{
                //    this.nowthreadCount = 0;
                //}
                Monitor.Exit(obj);
            }
        }

        #region 生成图表文件部分

        /// <summary>
        /// 开始创建图表文件
        /// </summary>
        /// <param name="account"></param>
        /// <param name="name"></param>
        /// <param name="dtLabel"></param>
        /// <returns></returns>
        private void FnCreateAccountChartFile(DataRow row)
        {
            string LABELID = string.Empty;
            string RELACODE = string.Empty;
            string RELANAME = string.Empty;
            string LABELTYPE = string.Empty;
            string WIDTH = string.Empty;
            string HEIGHT = string.Empty;
            string CHARTSTYLE = string.Empty;
            string LABELNAME = string.Empty;
            string GRAPHYID = string.Empty;
            try
            {
                LABELID = row["LABELID"].ToString();
                RELACODE = row["RELACODE"].ToString();
                RELANAME = row["RELANAME"].ToString();
                LABELTYPE = row["LABELTYPE"].ToString();
                WIDTH = row["WIDTH"].ToString();
                HEIGHT = row["HEIGHT"].ToString();
                CHARTSTYLE = row["CHARTSTYLE"].ToString();
                LABELNAME = row["LABELNAME"].ToString();
                GRAPHYID = row["GRAPHYID"].ToString();

                this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + RELACODE + " 在生成 " + LABELNAME + "图...");

                if (CHARTSTYLE == "0")
                {
                    FnListChartToDB(GRAPHYID, RELACODE, LABELID, LABELNAME, Convert.ToInt32(WIDTH), Convert.ToInt32(HEIGHT), chartFormat);              
                }
                else
                {
                    try
                    {
                        byte[] bytes = handler.DrawChart(CHARTSTYLE, RELACODE, LABELID, chartFormat, Convert.ToInt32(WIDTH), Convert.ToInt32(HEIGHT), "");
                        if (bytes != null && bytes != new byte[0])
                        {
                            GraphyDataInfo graphyData = new GraphyDataInfo();
                            #region Init Model
                            graphyData.ACCOUNTCODE = Convert.ToInt64(RELACODE);
                            graphyData.GRAPHYID = Convert.ToInt64(GRAPHYID);
                            graphyData.FILENAME = RELACODE + "_" + LABELID + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + "." + this.chartFormat;
                            graphyData.LABELID = Convert.ToInt64(LABELID);
                            graphyData.LABELNAME = LABELNAME;
                            #endregion
                            graphyData.NR = bytes;
                            graphyData.OPERATIONDATE = DateTime.Now.ToString();
                            queue.Push(graphyData);
                            this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + RELACODE + " 生成 " + RELANAME + "图已完毕");
                        }
                        else if (bytes == new byte[0])
                            chartData.UpdateControlState(GRAPHYID, LABELID);
                        else if (bytes == null)
                            chartData.UpdateControlTimes(GRAPHYID, LABELID);
                    }
                    catch(Exception ex)
                    {
                        chartData.UpdateControlTimes(GRAPHYID, LABELID);
                        this.WriteLog("\r\n－－" + ex.Message);
                    }
                }

                #region 注释1
                //foreach (DataRow dr in row)
                //{
                //    labelname = dr["NAME"].ToString();
                //    style = dr["CHARTSTYLE"].ToString();
                //    this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + account + " 在生成 " + labelname + "图...");

                //    if (CHARTSTYLE == "0")
                //    {
                //        FnListChartToDB(id, account, dr["SECID"].ToString(), labelname, Convert.ToInt32(dr["WIDTH"]), Convert.ToInt32(dr["HEIGHT"]), chartFormat);
                //    }
                //    else
                //    {
                //        byte[] bytes = handler.DrawChart(dr["CHARTSTYLE"].ToString(), account, dr["SECID"].ToString(), chartFormat, Convert.ToInt32(dr["WIDTH"]), Convert.ToInt32(dr["HEIGHT"]), "");
                //        if (bytes != null)
                //        {
                //            GraphyDataInfo graphyData = new GraphyDataInfo();
                //            #region Init Model
                //            graphyData.ACCOUNTCODE = Convert.ToInt64(account);
                //            graphyData.GRAPHYID = Convert.ToInt64(id);
                //            graphyData.FILENAME = account + "_" + dr["SECID"].ToString() + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + "." + this.chartFormat;
                //            graphyData.LABELID = Convert.ToInt64(dr["SECID"]);
                //            graphyData.LABELNAME = labelname;
                //            #endregion
                //            graphyData.NR = bytes;
                //            graphyData.OPERATIONDATE = DateTime.Now.ToString();
                //            queue.Push(graphyData);
                //            this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + account + " 生成 " + labelname + "图已完毕");
                //        }
                //    }                   
                //}
                #endregion
            }
            catch (Exception exception)
            {
                object obj = this.errorcount;
                Monitor.Enter(obj);
                this.errorcount++;
                Monitor.Exit(obj);
                this.tbAutoInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + RELACODE + " 在生成 " + LABELNAME + " 时出错，错误信息为：\r\n" + exception.ToString());
                this.WriteLog("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + RELACODE + " 在生成 " + LABELNAME + " 时出错，错误信息为：\r\n" + exception.ToString());
            }
        }
        #endregion

        #region 托盘相应功能

        // 窗体关闭事件
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        #endregion

        // 写执行日志
        public void WriteLog(string logMessage)
        {
            string path = Application.StartupPath + @"\log" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
            try
            {
                StreamWriter writer = File.AppendText(path);
                writer.Write(logMessage);
                writer.Flush();
                writer.Close();
            }
            catch
            {
                if (!File.Exists(path))
                {
                    File.Create(path).Close();
                }
                else
                {
                    File.SetAttributes(path, FileAttributes.Normal);
                }
            }
        }

        private void TSMItemHx_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            Hide();
        }

        private void notifyIcon_DoubleClick(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
            Activate();
            Focus();
        }

        private void notifyIcon_MouseClick(object sender, MouseEventArgs e)
        {
            //if (e.Button == System.Windows.Forms.MouseButtons.Right)
            //{
            //    if (WindowState == FormWindowState.Normal || Focused == true)
            //    {
            //        TSMItemOpen.Visible = false;
            //        TSMItemClose.Visible = true;
            //        TSMItemExit.Visible = true;
            //    }
            //    else if (WindowState == FormWindowState.Minimized || Focused == false)
            //    {
            //        TSMItemOpen.Visible = true;
            //        TSMItemClose.Visible = false;
            //        TSMItemExit.Visible = true;
            //    }
            //    else
            //    {
            //        TSMItemOpen.Visible = false;
            //        TSMItemClose.Visible = true;
            //        TSMItemExit.Visible = true;
            //    }
            //}
        }

        private void TSMItemOpen_Click(object sender, EventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
            Activate();
            Focus();
        }

        private void TSMItemClose_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            Hide();
        }

        private void TSMItemExit_Click(object sender, EventArgs e)
        {
            //notifyIcon.Visible = false;
            //notifyIcon.Dispose();
            Application.Exit();
        }

        private void btnExitSys_Click(object sender, EventArgs e)
        {
            //notifyIcon.Visible = false;
            //notifyIcon.Dispose();
            Application.Exit();
        }

        // 保存自动化程序设置
        private void btnSaveSet_Click(object sender, EventArgs e)
        {

        }

        // 处理图表入库轮询
        private void timer3_Tick(object sender, EventArgs e)
        {
            timer3.Interval = Convert.ToInt32(intevalToDB);

            timer3.Start();
            object obj = this.errorcount;
            Monitor.Enter(obj);
            this.errorcount = 0;
            Monitor.Exit(obj);
            FnChart2DB();
        }

        // 排队将图表插入数据库
        private void FnChart2DB()
        {
            string errorinfo = "";
            Monitor.Enter(queue);
            try
            {
                while (queue != null && queue.Count > 0)
                {
                    GraphyDataInfo data = (GraphyDataInfo)queue.Pop();
                    if (chartData.InsertChartToDB(data, out errorinfo))
                        this.tbToDBInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + data.ACCOUNTCODE.ToString() + " 的 " + data.LABELNAME + "图已入库");
                    else
                    {
                        this.tbToDBInfo.AppendText("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + data.ACCOUNTCODE.ToString() + " 的 " + data.LABELNAME + " 入库时出错，错误信息为：\r\n" + errorinfo);
                        this.WriteLog("\r\n－－" + DateTime.Now.ToString() + " 客户号：" + data.ACCOUNTCODE.ToString() + " 的 " + data.LABELNAME + " 入库时出错，错误信息为：\r\n" + errorinfo);
                    }
                }
            }
            catch (Exception exception)
            {
                this.tbToDBInfo.AppendText("\r\n－－" + exception.ToString());
                this.WriteLog("\r\n－－" + exception.ToString());
            }
            finally
            {
                Monitor.Exit(queue);               
            }                       
        }
       
        public void FnListChartToDB(string id, string account, string labelid, string labelname, int width, int height, string format)
        {
            try
            {
                bool b = false;
                string address = string.Empty;
                foreach (DataRow row in this.dtList.Select("ID=" + labelid))
                {
                    if (row["TYPE"].ToString().Equals(""))
                        return;
                    else if (row["TYPE"].ToString().Equals("0"))
                        b = true;
                    else if (row["TYPE"].ToString().Equals("1"))
                        b = chartData.IsHoldByStockType("STOCK", account);
                    else if (row["TYPE"].ToString().Equals("2"))
                        b = chartData.IsHoldByStockType("FUND", account);
                    else
                        return;

                    if (b)
                    {
                        address = row["ADDRESS"].ToString();
                        string url = "http://" + "localhost/hxzq/Chart/" + address + ".aspx";
                        string filename = account + "_" + labelid.ToString() + "_" + DateTime.Now.ToString("yyyyMMddhhmmss") + "." + format;
                        frmBrowser browser = new frmBrowser(url, account, Convert.ToInt32(id), labelid, labelname, width, height, filename, format, this);

                        browser.Visible = false;
                        browser.Hide();
                    }
                }
            }
            catch (Exception ex)
            {
                this.tbAutoInfo.AppendText("\r\n－－" + ex.Message);
                this.WriteLog("\r\n－－" + ex.Message);
            }
        }
    }
}